$(function() {
    class Goods_car {
        constructor() {
                this.str1 = 0
                this.vip = 0
                this.time = null;
            }
            // 按钮减少商品数量
        goodsNumCart(x, num) {
                num--
                if (num <= 0) {
                    num = 1
                }
                $(x).next().val(`${num}`)
                this.setGoodsPrice(x)
                this.database(x, num)
            }
            // 按钮增加商品数量

        goodsNumAdd(x, num) {
                num++
                if (num <= 0) {
                    num = 1
                }
                $(x).prev().val(`${num}`)
                this.setGoodsPrice(x)
                this.database(x, num)
            }
            // 修改数据库中的商品数量
        database(x, num) {
            let that = this
            clearTimeout(that.time)
            this.time = setTimeout(function() {
                let n = that.vip;
                let id = $(x).parent().parent().attr("class").slice(3);
                let goodsCount = num;
                console.log(id, goodsCount);
                $.get("../php/goodsAndShoppingCart/updateGoodsCount.php", {
                    "vipName": `${n}`,
                    "goodsId": `${id}`,
                    "goodsCount": `${goodsCount}`
                }, function(resText, status) {
                    if (status.trim() == "success") {
                        console.log(resText);
                    }

                })
            }, 1000)

        }

        // 设置商品价格
        setGoodsPrice(x) {
                let pric = $(x).parent().prev().html().slice(1);
                let num = $(x).parent().children().eq(1).val();
                let goods_price = pric * num;
                $(x).parent().next().html(`￥${goods_price}`)
                this.setSumPrice()
                this.setSumGoods()
            }
            // 总商品价格
        setSumPrice() {
                let vb = 0;
                for (let i = 0; i < $(".good_num_").length; i++) {
                    // debugger
                    vb += $(".good_num_").eq(i).next().html().slice(1) / 1
                }
                // console.log(vb);
                $(".ends").children().eq(1).children("span").html(`￥${vb}`);

            }
            // 总商品数量
        setSumGoods() {
                let vb = 0;
                // debugger
                for (let i = 0; i < $(".good_num_").length; i++) {
                    // debugger
                    vb += $(".good_num_").eq(i).children().eq(1).val() / 1
                }
                // console.log(vb);

                $(".ends").children().eq(0).children("span").html(`￥${vb}`);

            }
            //获取商品
        getgoods() {
                let that = this
                    // let vipName = decodeURIComponent(`${location.search}`).slice(1, 21);
                let vipName = location.search.slice(1);
                this.vip = vipName
                $.get("../php/goodsAndShoppingCart/getShoppingCart.php", {
                    "vipName": `${vipName}`
                }, function(resText, status) {
                    if (status.trim() == "success") {
                        that.str1 = resText
                        that.dispose(resText);
                    }

                })

            }
            //获取数据后调用的
        dispose(data) {
                data = JSON.parse(data)
                console.log(data);
                let str = '';
                for (let i = 0; i < data.length; i++) {
                    let n = data[i].goodsCount;
                    let price = data[i].goodsPrice;
                    let sumPrice = n * price
                    str += `<li class="id_${data[i].goodsId}">
                <span class="check"><input type="checkbox"></span>
                <div class="goods_img_l"><img src="${data[i].goodsImg}" alt=""></div>
                <div class="good_news_l"><p>${data[i].goodsDesc}</p>
                    <select>
                        <option value="">黑色</option>
                        <option value="">蓝色</option>
                        <option value="">白色</option>
                        <option value="">粉色</option>
                    </select>
                    <select>
                        <option value="">39码</option>
                        <option value="">40码</option>
                        <option value="">41码</option>
                        <option value="">42码</option>
                    </select>
                </div>
                <div class="good_price">￥${data[i].goodsPrice}</div>
                <div class="good_num_"><input type="button" value="-" class="cart"><input type="text" value="${data[i].goodsCount}"><input type="button" value="+" class="add">
                    <span>有货</span></div>
                <div class="add_goods">￥${sumPrice}</div>
                <div class="remove_goods"><a href="#">删除</a></div>
            </li>`
                }
                $(".list").html(`${str}`)
                this.goodscount()
                this.goodsadd()
                this.setSumPrice()
                this.setSumGoods()
                this.selectAll()
                this.ifSelect()
                this.removeGoods()
                this.selectEemove()
            }
            // 减少货物的点击事件
        goodscount() {
                let that = this
                $(".good_num_").children(".cart").click(function() {
                    let num = $(this).next().val();
                    let x = this;
                    that.goodsNumCart(x, num)
                })
            }
            // 增加商品的点击事件
        goodsadd() {
                let that = this
                $(".list").find(".good_num_").children(".add").click(function() {
                    // debugger
                    let num = $(this).prev().val();
                    let x = this;
                    that.goodsNumAdd(x, num)
                })
            }
            // 全选按钮
        selectAll() {
                $(".Select_all").children().click(function() {
                    let flag = $(this).prop("checked")
                    if (flag) {
                        $(".check").children().prop("checked", true)
                        $(".Select_all").children().prop("checked", true)
                    } else {
                        $(".check").children().prop("checked", false)
                        $(".Select_all").children().prop("checked", false)
                    }
                })
            }
            // 商品是否选择影响全选按钮
        ifSelect() {
                $(".check").children().click(function() {
                    let num = 0
                    for (let i = 0; i < $(".check").length; i++) {
                        let x = $(".check").eq(i).children().prop("checked");
                        if (x) {
                            num++
                        }
                    }
                    if (num == $(".check").length) {
                        $(".Select_all").children().prop("checked", true)
                    } else {
                        $(".Select_all").children().prop("checked", false)
                    }
                })
            }
            // 删除商品
        removeGoods() {
                let that = this
                $(".remove_goods").children().click(function() {
                    $(this).parent().parent().remove();
                    that.setSumPrice()
                    that.setSumGoods()
                    let x = $(this).parent().parent().attr("class").slice(3)
                        // let vipName = $(".good_news_l").children("P").text().slice(0, 21)
                        // console.log(vipName);
                    that.removes(x)

                })
            }
            // 发请求删除数据库中的商品
        removes(goodsId) {
                let that = this;
                $.get("../php/goodsAndShoppingCart/deleteGoods.php", {
                    "goodsId": `${goodsId}`,
                    "vipName": `${that.vip}`
                }, function(resText, status) {
                    if (status == "success") {
                        console.log(resText);
                    }

                })
            }
            // 删除选中商品
        selectEemove() {
            let that = this
            $(".remove_select").click(function() {
                for (let i = 0; i < $(".check").length; i++) {
                    let x = $(".check").eq(i).children().prop("checked");
                    if (x) {
                        let x = $(".check").parent().attr("class").slice(3)
                        that.removes(x)
                        $(".check").eq(i).parent().remove()
                        i--
                    }
                }
                that.setSumPrice()
                that.setSumGoods()
            })
        }
        evenbild() {
            this.goodscount()
            this.goodsadd()
            this.setSumPrice()
            this.setSumGoods()
            this.selectAll()
            this.ifSelect()
            this.removeGoods()
            this.selectEemove()
            this.getgoods()
        }


    }
    let g = new Goods_car()
    g.evenbild()




})